package com.ruoyi.customerSample.service.impl;

import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.DictUtils;
import com.ruoyi.common.utils.uuid.IdUtils;

import com.ruoyi.customerSample.domain.SampleTypeBill;
import com.ruoyi.customerSample.domain.SampleTypeDetails;
import com.ruoyi.customerSample.mapper.SampleTypeBillMapper;
import com.ruoyi.customerSample.mapper.SampleTypeDetailsMapper;
import com.ruoyi.customerSample.service.ISampleTypeBillService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 样品分类保存单据Service业务层处理
 * 
 * @author ruoyi
 * @date 2023-05-15
 */
@Service
public class SampleTypeBillServiceImpl implements ISampleTypeBillService
{
    @Autowired(required = false)
    private SampleTypeBillMapper sampleTypeBillMapper;

    @Autowired(required = false)
    private SampleTypeDetailsMapper sampleTypeDetailsMapper;

    /**
     * 查询样品分类保存单据
     * 
     * @param id 样品分类保存单据ID
     * @return 样品分类保存单据
     */
    @Override
    public SampleTypeBill selectSampleTypeBillById(String id)
    {
        SampleTypeBill sampleTypeBill = sampleTypeBillMapper.selectSampleTypeBillById(id);
        sampleTypeBill.setSampleType(DictUtils.getDictLabel(sampleTypeBill.getItemType(), sampleTypeBill.getSampleType(), ""));
        sampleTypeBill.setItemType(DictUtils.getDictLabel("item_type", sampleTypeBill.getItemType(), ""));
        sampleTypeBill.setList(sampleTypeDetailsMapper.selectSampleTypeDetailsByTypeId(id));
        return sampleTypeBill;
    }

    /**
     * 查询样品分类保存单据列表
     * 
     * @param sampleTypeBill 样品分类保存单据
     * @return 样品分类保存单据
     */
    @Override
    public List<SampleTypeBill> selectSampleTypeBillList(SampleTypeBill sampleTypeBill)
    {
        List<SampleTypeBill> sampleTypeBillList = sampleTypeBillMapper.selectSampleTypeBillList(sampleTypeBill);
        if(sampleTypeBillList.size() > 0) {
            for(SampleTypeBill sampleType : sampleTypeBillList) {
                sampleType.setList(sampleTypeDetailsMapper.selectSampleTypeDetailsByTypeId(sampleType.getId()));
            }
        }
        return sampleTypeBillList;
    }

    /**
     * 新增样品分类保存单据
     * 
     * @param sampleTypeBill 样品分类保存单据
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int insertSampleTypeBill(SampleTypeBill sampleTypeBill)
    {
        String id = IdUtils.fastSimpleUUID();
        sampleTypeBill.setId(id);
        int ret = sampleTypeBillMapper.insertSampleTypeBill(sampleTypeBill);
        if(ret > 0) {
            //更新明细
            if(sampleTypeBill.getList().size() > 0) {
                for(SampleTypeDetails sampleTypeDetails : sampleTypeBill.getList()) {
                    sampleTypeDetails.setSampleTypeId(id);
                    sampleTypeDetails.setId(IdUtils.fastSimpleUUID());
                }
                sampleTypeDetailsMapper.batchSampleTypeDetailBill(sampleTypeBill.getList());
            }
        }
        return ret;
    }

    /**
     * 修改样品分类保存单据
     * 
     * @param sampleTypeBill 样品分类保存单据
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int updateSampleTypeBill(SampleTypeBill sampleTypeBill)
    {
        sampleTypeBill.setUpsampleTime(DateUtils.getNowDate());
        //删除原本的内容
        if(sampleTypeBill.getList().size() > 0) {
            //根据id删除
            sampleTypeDetailsMapper.deleteSampleTypeDetailsByTypeId(sampleTypeBill.getId());
            for(SampleTypeDetails sampleTypeDetails : sampleTypeBill.getList()) {
                sampleTypeDetails.setSampleTypeId(sampleTypeBill.getId());
                sampleTypeDetails.setId(IdUtils.fastSimpleUUID());
            }
            sampleTypeDetailsMapper.batchSampleTypeDetailBill(sampleTypeBill.getList());
        }
        return sampleTypeBillMapper.updateSampleTypeBill(sampleTypeBill);
    }

    /**
     * 批量删除样品分类保存单据
     * 
     * @param ids 需要删除的样品分类保存单据ID
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int deleteSampleTypeBillByIds(String[] ids)
    {
        for(int i = 0; i < ids.length; i++) {
            sampleTypeDetailsMapper.deleteSampleTypeDetailsByTypeId(ids[i]);
        }
        return sampleTypeBillMapper.deleteSampleTypeBillByIds(ids);
    }

    /**
     * 删除样品分类保存单据信息
     * 
     * @param id 样品分类保存单据ID
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int deleteSampleTypeBillById(String id)
    {
        sampleTypeDetailsMapper.deleteSampleTypeDetailsByTypeId(id);
        return sampleTypeBillMapper.deleteSampleTypeBillById(id);
    }
}
